<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <#include "../common/header.ftl"/>
    <script type="text/javascript" src="/js/plugins/treeview/bootstrap-treeview.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/treeview/bootstrap-treeview.min.css" type="text/css"/>
    <title>骡窝窝系统管理平台</title>
    <script type="text/javascript">
        $(function(){
            $.get('/region/listByParentId.do',{type:"tree"},function (data) {
                $('#treeview').treeview({
                    data: [{text:"全部地区",nodes:data}],
                    showTags:true,
                    //node是当前点击的节点
                    lazyLoad:function (node) {
                        $.get('/region/listByParentId.do',{parentId:node.id,type:"tree"},function (list) {
                            //把数据设置到treeview中
                            $('#treeview').treeview('addNode',[list,node]);
                        })
                    },
                    //选择节点事件
                    onNodeSelected:function (event,data) {
                        $.get('/region/listByParentId.do',{parentId:data.id},function (list) {
                            var temp = "";
                            $.each(list,function (index, ele) {
                                temp+="<tr><td>"+(index+1)+"</td><td>"+ele.name+"</td><td>"
                                        +"<a class=\"btn btn-info btn-xs inputBtn\" href=\"javascript:\"\n" +
                                        "data-json="+ele.json+">\n" +
                                        "<span class=\"glyphicon glyphicon-pencil\"></span>编辑\n" +
                                        "</a>\n"
                                        +"</td></tr>";
                            });
                            $("tbody").html(temp);
                            inputClick();
                        });
                    }
                });
            });
        });
    </script>
</head>

<body>
<div class="container">
<#include "../common/top.ftl"/>
    <div class="row">
        <div class="col-sm-3">
		<#assign currentMenu = "region" />
				<#include "../common/menu.ftl" />
        </div>
        <div class="col-sm-9">
            <div class="page-header">
                <h3>地区管理</h3>
            </div>
            <form id="searchForm" class="form-inline" method="post" action="/region/list.do">
                <input type="hidden" name="currentPage" id="currentPage" value="1"/>
                <a href="javascript:" class="btn btn-success inputBtn">
                    <span class="glyphicon glyphicon-plus"></span> 添加
                </a>
            </form>
            <div class="row">
                <div class="col-sm-4">
                    <div id="treeview"></div>
                </div>
                <div class="col-sm-8">
                    <table class="table">
                        <thead>
                        <tr>
                            <th>编号</th>
                            <th>名字</th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">编辑</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/region/saveOrUpdate.do" method="post" id="editForm">
                    <input type="hidden" name="id">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">地区名称：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="name" placeholder="请输入地区的名称">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label">上级地区：</label>
                        <div class="col-sm-6">
                            <input type="hidden" class="form-control" name="parent.id"/>
                            <input type="text" class="form-control" id="parentName" name="parentName" readonly/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">状态：</label>
                        <div class="col-sm-6">
                            <select class="form-control" name="state">
                                <option value="0">普通</option>
                                <option value="1">推荐</option>
                            </select>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary submitBtn">保存</button>
            </div>
        </div>
    </div>
</div>
<script>
    function inputClick(){
        $(".inputBtn").click(function () {
            // 4步走,清空,取值,回显,显示
            // 1. 清空
            $('#myModal input').val('');
            // 2. 取值
            var obj = $(this).data('json');
            // 3. 编辑回显
            if (obj) {
                $('#myModal input[name="id"]').val(obj.id);
                $('#myModal input[name="name"]').val(obj.name);
                $('#myModal input[name="parent.id"]').val(obj.parentId);
                $('#myModal input[name="parentName"]').val(obj.parentName);
                $('#myModal select[name="state"]').val(obj.state);
            }else {
                //获取被选中的元素
                var arr = $('#treeview').treeview('getSelected');
                console.log(arr);
                if (arr.length>0){
                    $('#myModal input[name="parent.id"]').val(arr[0].id);
                    $('#myModal input[name="parentName"]').val(arr[0].text);
                }
            }
            // 4. 显示
            $('#myModal').modal('show');
        });
    }

    $(function () {
       inputClick();
        // 使用jQueryform修改模态框的提交方式-ajax方式,在表单提交之前
        $('#editForm').ajaxForm(function (data) {
            if (data.success) {
                // 操作成功,刷新页面
                location.reload();
            } else {
                alert(data.msg);
            }
        });
        // 模态框的表单提交
        $('.submitBtn').click(function () {
            $('#editForm').submit();
        });
    });
</script>
</body>
</html>